package tools;

import funcktion.BookManage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Date;
import java.sql.Timestamp;


public class BorrowGUI extends JFrame {
    private JTextField bookIdTextField;
    private JButton borrowButton;

    public BorrowGUI(int userId) {
        setTitle("借书");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2, 10, 10));

        JLabel bookIdLabel = new JLabel("图书编号");
        bookIdTextField = new JTextField();
        borrowButton = new JButton("确定借书");

        borrowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                borrowBookInfo(userId);
            }
        });

        add(bookIdLabel);
        add(bookIdTextField);
        add(new JLabel());
        add(borrowButton);

        pack();
        setLocationRelativeTo(null);
    }

    private void borrowBookInfo(int userId) {
        String bookId = bookIdTextField.getText();

        if (bookId.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写编号");
        } else {
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
                PreparedStatement stmt = conn.prepareStatement("SELECT bookId,availableCopies FROM Book WHERE bookId = ?");
                stmt.setString(1, bookId);

                ResultSet rs = stmt.executeQuery();
                if (rs.next()) {
                    int availableCopies = rs.getInt("availableCopies");
                    if (availableCopies == 0) {
                        JOptionPane.showMessageDialog(this, "库存不足");
                    } else {
                        int newAvailableCopies = availableCopies - 1;
                        // 更新图书库存
                        PreparedStatement stmt1 = conn.prepareStatement("UPDATE Book SET availableCopies = ? WHERE bookId = ?");
                        stmt1.setInt(1, newAvailableCopies);
                        stmt1.setString(2, bookId);
                        stmt1.executeUpdate();

                        // 获取当前时间
                        Date currentDate = new Date();
                        // 将当前时间转换为 Timestamp 类型
                        Timestamp timestamp = new Timestamp(currentDate.getTime());


                        // 插入借书记录
                        PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO Borrow (userId, bookId,borrowTime) VALUES (?, ?, ?)");
                        stmt2.setInt(1, userId);
                        stmt2.setString(2, bookId);
                        stmt2.setTimestamp(3, timestamp);
                        stmt2.executeUpdate();

                        // 更新用户借书数量
                        PreparedStatement stmt3 = conn.prepareStatement("UPDATE User SET borrowedBooks = borrowedBooks + 1 WHERE userId = ?");
                        stmt3.setInt(1, userId);
                        stmt3.executeUpdate();

                        JOptionPane.showMessageDialog(this, "借书成功");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "图书不存在");
                }
                setVisible(false);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, "借书失败：" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

}
